﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using COFE.Core;

namespace COFE.Data.UnitTest
{
    [TestFixture]
    public class TestProfileRoot
    {
        #region Constructor

        public static string TestVolumeKey = @"Temp";
        public static string TestVolumePath = @"C:\temp\TestCache";
        public static string TestPath = "A\\B";

        static TestProfileRoot()
        {
            COFEDBShared.Register(null, true);
            DummyDBPropertyProvider.EnableDummySecondaryPropertyProvider();
        }

        #endregion

        #region Methods

        public void CreateUser()
        {
            if (DBUserProfile.Exists("TestProfileRoot"))
                DBUserProfile.Remove("TestProfileRoot");
            IUserProfile user = DBUserProfile.Create("TestProfileRoot", "TestProfileRoot");
            if (COFEShared.VolumeRegistrar.LookupRegisterVolume(TestVolumeKey) == null)
                COFEShared.VolumeRegistrar.RegisterVolume(
                    COFEShared.VolumeRegistrar.ConstructVolume("FSI", TestVolumeKey, TestVolumePath, 
                    Core.Profile.VolumeAccessMode.All));

            user.RegisterVolume(TestVolumeKey);


        }

        public IUserProfile LoginUser()
        {
            var user = DBUserProfile.Login("TestProfileRoot", "TestProfileRoot");
            Assert.IsNotNull(user);
            return user;
        }

        [Test]
        public IDirectoryInfo LookupProfileRootDirectory_UsingPath()
        {
            CreateUser();
            var user = LoginUser();

            IDirectoryInfo retVal = user.TryParsePath("", Core.Profile.EntryConstructionFlags.Default) as IDirectoryInfo;
            Assert.IsNotNull(retVal);
            return retVal;
        }

        [Test]
        public IDirectoryInfo LookupProfileRootDirectory_UsingGuid()
        {
            IDirectoryInfo retVal = LookupProfileRootDirectory_UsingPath();
            Assert.IsNotNull(retVal);
            var user = LoginUser();
            
            retVal = user.TryParseGuid(retVal.GetId()) as IDirectoryInfo;
            Assert.IsNotNull(retVal);
            
            return retVal;
        }

        [Test]
        public void ListProfileRootDirectory()
        {
            IDirectoryInfo retVal = LookupProfileRootDirectory_UsingGuid();
            Assert.IsNotNull(retVal);
            var user = LoginUser();

            var children = retVal.GetFileSystemInfos();

            foreach (var child in children)
                Console.WriteLine(child.ParseName);
            Assert.AreEqual(1, children.Length);
            
        }

        TimeSpan Benchmark(int indent, string name, Action action)
        {
            if (indent == 0)
                Console.WriteLine(Indent(indent) + name);
            else Console.Write(Indent(indent) + name + " - ");

            DateTime startTime = DateTime.UtcNow;
            action.Invoke();
            DateTime endTime = DateTime.UtcNow;
            TimeSpan timeUsed = endTime.Subtract(startTime);
            if (indent == 0)
                Console.WriteLine(Indent(indent) + "Total time spent - " + timeUsed.TotalSeconds.ToString() + "s");
            else Console.WriteLine(timeUsed.TotalSeconds.ToString() + "s");
            return timeUsed;

        }

        
        public static string Indent(int count)
        {
            StringBuilder sb = new StringBuilder();
            for (var i = 0; i < count; i++)
                sb.Append(" ");
            return sb.ToString();
        }

        public void Test()
        {
            Benchmark(0, "LookupProfileRootDirectory_UsingPath", () => { LookupProfileRootDirectory_UsingPath(); });
            Console.WriteLine();

            Benchmark(0, "LookupProfileRootDirectory_UsingGuid", () => { LookupProfileRootDirectory_UsingGuid(); });
            Console.WriteLine();

            Benchmark(0, "ListProfileRootDirectory", () => { ListProfileRootDirectory(); });
            Console.WriteLine();
        }
        #endregion

        #region Data

        #endregion

        #region Public Properties

        public string parsableVolumeKey { get { return "{" + TestVolumeKey + "}"; } }

        #endregion
    }
}
